﻿using BLL.Organization;
using BLL.Origins;
using Common;
using Enums;
using Enums.Sys;
using Model.CodeManage;
using Model.Origins;
using Model.Sys;
using PagedList;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;

namespace DAGG.Controllers.CodeManage
{
    public class BindCodeController : BaseController
    {
       OriginsTempletBLL bll = new OriginsTempletBLL();
       EnterpriseBLL enterbll = new EnterpriseBLL();
        //
        // GET: /BindCode/
       public ActionResult Index(string term = "", int page = 1)
        {
            SysUserModel userModel = base.GetUserInfo();
            var enterId = userModel.EnterpriseId;
            string strWhere = string.Empty;
            //递归获取其所属子公司和子公司的子公司
            if (EnumManagement.GetEnumValue(SysRoleEnum.SuperManageRole) == userModel.RoleId)//管理员角色 查看全部
                strWhere += "  ";
            else
            {
                var listWhere = string.Join(",", enterbll.GetSonEnterprise(userModel.EnterpriseId).Select(obj => obj.ID));
                if (string.IsNullOrEmpty(listWhere))
                    strWhere += " and (oo.EnterpriseId  in (0))";
                else
                    strWhere += " and (oo.EnterpriseId in (" + listWhere + "))";
            }

            //page = 1;
            IPagedList<OriginsTempletModel> modelInfoList = bll.GetList(strWhere, term).ToPagedList(page, 10);
            ViewBag.PageList = modelInfoList;
            return View();
        }

        public ActionResult Edit(string id, string EnterpriseId)
        {
            
            ViewBag.id = id;//模板表的主键编号
            ViewBag.EnterpriseId = EnterpriseId;//模板表的机构id信息
            return View();
        }
        public ActionResult SaveData(BindCodeModel model)
        {
           
            if (model.IsContinuous)
            { //连续
                bll.UpdateDigitcode0(model);
            }
            else
            {
                if (model.BS == null) return Redirect("/BindCode/Edit");
                int i = bll.UpdateDigitcode(model);
            }
           
           
            return Redirect("/BindCode/Index");
        }
	}
}